package com.atguigu.order.controller;


import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.ResultCode;
import com.atguigu.order.entity.Order;
import com.atguigu.order.service.OrderService;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/order/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 保存订单信息
     *
     * @param courseId 课程id
     * @return 订单号
     */
    @ApiOperation("保存订单信息")
    @PostMapping("/saveEntity/{courseId}")
    public R saveEntity(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new GuLiException(ResultCode.NO_LOGIN, "未登录");
        }
        String orderNo = orderService.saveEntity(courseId, token);
        return R.ok().data(orderNo);
    }

    @GetMapping("/getOrderInfo/{orderNo}")
    public R getOrderInfo(@PathVariable String orderNo) {
        return R.ok().data(orderService.getOne(
                new LambdaQueryWrapper<Order>()
                        .eq(Order::getOrderNo, orderNo)));
    }

    @GetMapping("/getCourseOrderInfoByCourseId/{courseId}")
    public R getCourseOrderInfoByCourseId(@PathVariable String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new GuLiException(ResultCode.NO_LOGIN, "未登录");
        }
        Boolean isPay = orderService.getCourseOrderInfoByCourseId(courseId, token);
        return R.ok().data(isPay);
    }
}

